
/**
 * usersettings.cpp
 * 
 * UserSettings holds all user settings saved in the PC
 * 
 * Copyright (c) 2008 Harunobu Oyama <nobukichi at gmail.com>
 * Licensed under GNU GPL version 3
 *
 */

#include <QDir>
#include "textcipher.h"
#include "usersettings.h"



//! change CREDENTIAL_KEY_SEED to keep your password and encryption key secure
const char* const CREDENTIAL_KEY_SEED        = "i am a little bit sleepy now";



const char* const TWITTER_USER_NAME          = "twitterUserName";
const char* const ENCRYPTED_TWITTER_PASSWORD = "twitterPassword";
const char* const SAVE_TWITTER_PASSWORD      = "saveTwitterPassword";
const char* const GATEWAY_USER_NAME          = "gatewayUserName";
const char* const ENCRYPTED_ENCRYPTION_KEY   = "encryptionKey";
const char* const SAVE_ENCRYPTION_KEY        = "saveEncryptionKey";
const char* const USE_HTTP_PROXY             = "useHttpProxy";
const char* const HTTP_PROXY_ADDRESS         = "httpProxyAddress";
const char* const HTTP_PROXY_PORT            = "httpProxyPort";
const char* const PROXY_USER_NAME            = "proxyUserName";
const char* const ENCRYPTED_PROXY_PASSWORD   = "proxyPassword";
const char* const SAVE_PROXY_PASSWORD        = "saveProxyPassword";
const char* const AUTO_UPDATE_INTERVAL       = "autoUpdateInterval";
const char* const SAVE_LOG                   = "saveLog";
const char* const LOG_PATH                   = "logPath";



UserSettings* UserSettings::get()
{
	static UserSettings theInstance;
	return &theInstance;
}

UserSettings::UserSettings()
{
	settings.sync();

	if (saveTwitterPassword() && settings.contains(ENCRYPTED_TWITTER_PASSWORD)) {
		QString encryptedPassword = settings.value(ENCRYPTED_TWITTER_PASSWORD).toString();
		setTwitterPassword(decrypt(encryptedPassword));
	} else {
		setTwitterPassword("");
	}
	
	if (saveEncryptionKey() && settings.contains(ENCRYPTED_ENCRYPTION_KEY)) {
		QString encryptedEncKey = settings.value(ENCRYPTED_ENCRYPTION_KEY).toString();
		setEncryptionKey(decrypt(encryptedEncKey));
	} else {
		setEncryptionKey("");
	}

	if (saveProxyPassword() && settings.contains(ENCRYPTED_PROXY_PASSWORD)) {
		QString encryptedPassword = settings.value(ENCRYPTED_PROXY_PASSWORD).toString();
		setProxyPassword(decrypt(encryptedPassword));
	} else {
		setProxyPassword("");
	}
}

void UserSettings::save()
{
	if (saveTwitterPassword()) {
		QString encryptedPassword = encrypt(twitterPassword());
		settings.setValue(ENCRYPTED_TWITTER_PASSWORD, encryptedPassword);
	} else {
		settings.remove(ENCRYPTED_TWITTER_PASSWORD);
	}

	if (saveEncryptionKey()) {
		QString encryptedEncKey = encrypt(encryptionKey());
		settings.setValue(ENCRYPTED_ENCRYPTION_KEY, encryptedEncKey);
	} else {
		settings.remove(ENCRYPTED_ENCRYPTION_KEY);
	}

	if (saveProxyPassword()) {
		QString encryptedPassword = encrypt(proxyPassword());
		settings.setValue(ENCRYPTED_PROXY_PASSWORD, encryptedPassword);
	} else {
		settings.remove(ENCRYPTED_PROXY_PASSWORD);
	}

	settings.sync();
}



//! Twitter account related information

QString	UserSettings::twitterUserName() const
{
	return settings.value(TWITTER_USER_NAME).toString();
}

QString	UserSettings::twitterPassword() const
{
	return twitterPasswordCache;
}

bool UserSettings::saveTwitterPassword() const
{
	return settings.value(SAVE_TWITTER_PASSWORD).toBool();
}

void UserSettings::setTwitterUserName(QString usrName)
{
	settings.setValue(TWITTER_USER_NAME, usrName);
}

void UserSettings::setTwitterPassword(QString passwd)
{
	twitterPasswordCache = passwd;
}

void UserSettings::setSaveTwitterPassword(bool savePasswd)
{
	settings.setValue(SAVE_TWITTER_PASSWORD, savePasswd);
}



//! Group encryption related information

QString UserSettings::gatewayUserName() const
{
	return settings.value(GATEWAY_USER_NAME).toString();
}

QString UserSettings::encryptionKey() const
{
	return encryptionKeyCache;
}

bool UserSettings::saveEncryptionKey() const
{
	return settings.value(SAVE_ENCRYPTION_KEY).toBool();
}

void UserSettings::setGatewayUserName(QString gatewayName)
{
	settings.setValue(GATEWAY_USER_NAME, gatewayName);
}

void UserSettings::setEncryptionKey(QString encKey)
{
	encryptionKeyCache = encKey;
}

void UserSettings::setSaveEncryptionKey(bool saveKey)
{
	settings.setValue(SAVE_ENCRYPTION_KEY, saveKey);
}



//! Http proxy related information

bool UserSettings::useHttpProxy() const
{
	return settings.value(USE_HTTP_PROXY).toBool();
}

QString UserSettings::httpProxy() const
{
	return settings.value(HTTP_PROXY_ADDRESS).toString();
}

int UserSettings::httpProxyPort() const
{
	return settings.value(HTTP_PROXY_PORT).toInt();
}

void UserSettings::setUseHttpProxy(bool use)
{
	settings.setValue(USE_HTTP_PROXY, use);
}

void UserSettings::setHttpProxy(const QString& address)
{
	settings.setValue(HTTP_PROXY_ADDRESS, address);
}

void UserSettings::setHttpProxyPort(int port)
{
	settings.setValue(HTTP_PROXY_PORT, port);
}



//! Proxy authentication related information

QString	UserSettings::proxyUserName() const
{
	return settings.value(PROXY_USER_NAME).toString();
}

QString	UserSettings::proxyPassword() const
{
	return proxyPasswordCache;
}

bool UserSettings::saveProxyPassword() const
{
	return settings.value(SAVE_PROXY_PASSWORD).toBool();
}

void UserSettings::setProxyUserName(QString usrName)
{
	settings.setValue(PROXY_USER_NAME, usrName);
}

void UserSettings::setProxyPassword(QString password)
{
	proxyPasswordCache = password;
}

void UserSettings::setSaveProxyPassword(bool savePassword)
{
	settings.setValue(SAVE_PROXY_PASSWORD, savePassword);
}

int UserSettings::autoUpdateInterval() const
{
	return settings.value(AUTO_UPDATE_INTERVAL, QVariant(3)).toInt();
}

void UserSettings::setAutoUpdateInterval(int interval)
{
	settings.setValue(AUTO_UPDATE_INTERVAL, interval);
}

bool UserSettings::saveLog() const
{
	return settings.value(SAVE_LOG, QVariant(true)).toBool();
}

void UserSettings::setSaveLog(bool save)
{
	settings.setValue(SAVE_LOG, save);
}

QString UserSettings::logPath() const
{
	QString defaultPath = QDir::homePath() + "/nobitter_log";
	return settings.value(LOG_PATH, QVariant(defaultPath)).toString();
}

void UserSettings::setLogPath(const QString& path)
{
	settings.setValue(LOG_PATH, path);
}

//! helper functions

QString UserSettings::encrypt(QString plain)
{
	return TextCipher::encode(plain, CREDENTIAL_KEY_SEED);
}

QString UserSettings::decrypt(QString code)
{
	return TextCipher::decode(code, CREDENTIAL_KEY_SEED);
}

